/*
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

package net.sf.refactorit.ui.module.createmissingmethod;


import net.sf.refactorit.classmodel.BinTypeRef;
import net.sf.refactorit.ui.dialog.RitDialog;
import net.sf.refactorit.ui.module.RefactorItContext;
import net.sf.refactorit.ui.tree.InheritanceTreeModel;
import net.sf.refactorit.ui.tree.JClassTree;
import net.sf.refactorit.utils.SwingUtil;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeSelectionModel;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


/**
 *
 * @author  tanel
 */
public class BaseClassDialog {
  final RitDialog dialog;

  private RefactorItContext context;
  private BinTypeRef superType;

  private boolean okPressed;
  private JClassTree classTree;

  /** Creates new form BaseClassDialog */
  public BaseClassDialog(RefactorItContext context, BinTypeRef superType) {
    dialog = RitDialog.create(context);

    this.context = context;
    this.superType = superType;

    initComponents();    
    
    SwingUtil.initCommonDialogKeystrokes(dialog, okButton, cancelButton,
        helpButton, new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    } );
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  private void initComponents() {//GEN-BEGIN:initComponents
    GridBagConstraints gridBagConstraints;

    downPanel = new Panel();
    buttonPanel = new Panel();
    okButton = new JButton();
    cancelButton = new JButton();
    helpButton = new JButton();
    scrollPane = new JScrollPane();
    hierarchyPanel = new Panel();

    dialog.setTitle("Choose base class");
    dialog.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent evt) {
        closeDialog(evt);
      }
    });

    downPanel.setLayout(new GridBagLayout());

    okButton.setText("OK");
    okButton.setNextFocusableComponent(cancelButton);
    okButton.addActionListener(new ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        okButtonActionPerformed(evt);
      }
    });

    buttonPanel.add(okButton);

    cancelButton.setText("Cancel");
    cancelButton.setNextFocusableComponent(helpButton);
    cancelButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        cancelButtonActionPerformed(evt);
      }
    });

    buttonPanel.add(cancelButton);

    helpButton.setText("Help");
    helpButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
        helpButtonActionPerformed(evt);
      }
    });

    buttonPanel.add(helpButton);

    gridBagConstraints = new GridBagConstraints();
    gridBagConstraints.anchor = GridBagConstraints.EAST;
    gridBagConstraints.weightx = 1.0;
    gridBagConstraints.weighty = 1.0;
    gridBagConstraints.insets = new Insets(3, 10, 3, 20);
    downPanel.add(buttonPanel, gridBagConstraints);

    dialog.getContentPane().add(downPanel, BorderLayout.SOUTH);

    hierarchyPanel.setLayout(new BorderLayout());

    // add class hierarchy
    createHierarchyTable();
    scrollPane.setViewportView(hierarchyPanel);

    dialog.getContentPane().add(scrollPane, BorderLayout.CENTER);

    dialog.setSize(304, 212);
  }//GEN-END:initComponents

  private void createHierarchyTable() {
    TreeModel model = new InheritanceTreeModel(
        new InheritanceSourceClassNode(null, superType.getBinCIType()));
    classTree = new JClassTree(model, context);
    classTree.setRootVisible(true);
    classTree.getSelectionModel().setSelectionMode(TreeSelectionModel.
        SINGLE_TREE_SELECTION);
    InheritanceTreeModel.expandTree(classTree.getPathForRow(0), classTree);
    //classTree.setSelectionRow(rowOfOwner - 1);

    //setSelectedNode((InheritanceNode)classTree.getSelectionPath().getLastPathComponent(),
    //								rowOfOwner);

    //addListenerForClassTree(classTree, context);

    hierarchyPanel.add(classTree, BorderLayout.CENTER);
  }

  void cancelButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
    dialog.dispose();
  }//GEN-LAST:event_cancelButtonActionPerformed

  void okButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
    okPressed = true;
    dialog.dispose();
  }//GEN-LAST:event_okButtonActionPerformed

  void helpButtonActionPerformed(ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
    // Add your handling code here:
  }//GEN-LAST:event_helpButtonActionPerformed

  /** Closes the dialog */
  void closeDialog(WindowEvent evt) {//GEN-FIRST:event_closeDialog
    dialog.dispose();
  }//GEN-LAST:event_closeDialog

  public boolean isOkPressed() {
    return this.okPressed;
  }

  public BinTypeRef getSelectedTypeRef() {
    Object selection = classTree.getSelectionPath().getLastPathComponent();
    System.out.println("getLastPathComponent: " + selection);
    if (selection == null) {
      return null;
    }

    return ((InheritanceSourceClassNode) selection).getBinCIType().getTypeRef();
  }

//  public static void main(String args[]) {
//    new BaseClassDialog(new JFrame(), true).show();
//  }

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private Panel hierarchyPanel;
  private JScrollPane scrollPane;
  private JButton okButton;
  private JButton cancelButton;
  private Panel buttonPanel;
  private JButton helpButton;
  private Panel downPanel;
  // End of variables declaration//GEN-END:variables

  public void show() {
    dialog.show();
  }
}
